<?php
if (!defined('ENGINE_NAME')) die ("You can't access this file directly!");

/**
* Get an array with list of files in specified directory
* @author NG CMS Team, Bogdan Tkachenko (http://bogus.mail.ua)
* @param sPath Directory path
* @param sExt Files extension to show
* @return Array with list of files in specified directory or false if can not get file list
*/
function ListFiles($sPath, $sExt = '')
{
	if (!is_dir($sPath))
	{
		return false;
	}

	if (!$hHandle = opendir($sPath))
	{
		return false;
	}

	$aFileList = array();	
	while (false !== ($sFile = readdir($hHandle)))
	{
		if ($sFile!='.' && $sFile!='..')
		{
			if ($sExt)
			{
				if (preg_match("%$sExt$%", $sFile))
				{
					$aFile = explode(".", $sFile);
					$aFileList["$aFile[0]"]= $aFile[0];
				}
			}
			else
			{
				$aFileList[$sFile]= $sFile;
			}
		}
	}
	
	closedir($hHandle);
	
	return $aFileList;
}

/**
* Get client IP address
* @author NG CMS Team, Bogdan Tkachenko (http://bogus.mail.ua)
* @return Client IP address
*/
function GetIP()
{
	if (getenv("REMOTE_ADDR"))
	{
		return getenv("REMOTE_ADDR");
	}
	elseif ($_SERVER["REMOTE_ADDR"])
	{
		return $_SERVER['REMOTE_ADDR'];
	}
	return "unknown";
}

/**
* Plugins priority sort function for usort()
* @author Bogdan Tkachenko (http://bogus.mail.ua)
* @param aFirstPlugin First plugin array
* @param aSecondPlugin Second plugin array
* @return Plugins priority comparsion
*/
function SortPlugins($aFirstPlugin, $aSecondPlugin)
{
	if ($aFirstPlugin['Prio'] == $aSecondPlugin['Prio'])
	{
		return 0;
	}
	
	return ($aFirstPlugin['Prio'] > $aSecondPlugin['Prio'])?-1:1;
}

/**
* Remove slashes in variable
* @author Bogdan Tkachenko (http://bogus.mail.ua)
* @param uVar Variable to filter
* @return Filtered variable
*/
function RemoveSlashes($uVar)
{
	if (is_array($uVar))
	{
		$uVar = array_map('RemoveSlashes', $uVar);
	}
	elseif (!empty($uVar) && is_string($uVar))
	{
		$uVar = stripslashes($uVar);
	}
	return $uVar;
}

/**
* Filter HTML special characters in a variable
* @author Bogdan Tkachenko (http://bogus.mail.ua)
* @param uVar Variable to filter
* @return Filtered variable
*/
function FilterHtmlSpecialChars($uVar)
{
	if (is_array($uVar))
	{
		$uVar = array_map('FilterHtmlSpecialChars', $uVar);
	}
	elseif (!empty($uVar) && is_string($uVar))
	{
		$uVar = htmlspecialchars($uVar);
	}
	return $uVar;
}
/**
* Convert array values to array keys
* e.g. $x = array('a','b','c'); to $x['a']['b']['c'] = 0;
* @author r00d1k
* @param array Array to convert

*/
function ArrayRecursion($aArray, $uVal)
{
	$aSecondArray[current($aArray)] = $uVal;
	unset($aArray[key($aArray)]);
	if (count($aArray))
	{
    	$aSecondArray = ArrayRecursion($aArray,$aSecondArray);
    }
    return $aSecondArray;
}
